package com.lzh.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzh.commonutils.CommonResult;
import com.lzh.commonutils.JwtUtils;
import com.lzh.order.entity.TOrder;
import com.lzh.order.service.TOrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author lizuhu
 * @since 2021-05-29
 */
@CrossOrigin
@RestController
@RequestMapping("/order")
public class TOrderController {
    @Autowired
    private TOrderService tOrderService;

    /**
     * 创建订单，
     * @param courseId 课程id
     * @param request token 用于获取当前用户信息
     * @return
     */
    @GetMapping("/createOrder")
    public CommonResult createOrder(@RequestParam String courseId, HttpServletRequest request) {
        String orderId = tOrderService.createOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return CommonResult.ok().data("orderId",orderId);
    }

    /**
     * 根据订单id获取订单信息
     * @param orderId
     * @return
     */
    @ApiOperation(value = "根据订单id获取订单")
    @GetMapping("/getOrder")
    public CommonResult getOrder(@RequestParam String orderId) {
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        TOrder order = tOrderService.getOne(wrapper);
        return CommonResult.ok().data("item",order);
    }

    /**
     * 查询订单是否支付
     * @param userId 用户id
     * @param courseId 课程id
     * @return
     */
    @ApiOperation(value = "根据用户id和课程id查询订单信息")
    @GetMapping("/isBuy")
    public boolean isBuyCourse(@RequestParam String userId,@RequestParam String courseId) {
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        //根据条件查询
        wrapper.eq("member_id",userId);
        wrapper.eq("course_id",courseId);
        wrapper.eq("status",1);
        int count = tOrderService.count(wrapper);
        if (count > 0) {
            return true;
        }else {
            return false;
        }
    }
}

